#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QFont>
#include <QFontDialog>
#include <QMessageBox>
#include <QString>
#include <QTextStream>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include <QColorDialog>
#include <QColor>
#include <QDateTime>
#include <QCoreApplication>
#include <QDesktopServices>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Untitled");
    QObject::connect(ui->actionNew,SIGNAL(triggered()),this,SLOT(newFile()));
    QObject::connect(ui->actionOpen,SIGNAL(triggered()),this,SLOT(openFile()));
    QObject::connect(ui->actionSave,SIGNAL(triggered()),this,SLOT(saveFile()));
    QObject::connect(ui->actionSave_As,SIGNAL(triggered()),this,SLOT(saveASFile()));
    QObject::connect(ui->actionPrint,SIGNAL(triggered()),this,SLOT(printFile()));
    QObject::connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(close()));

    QObject::connect(ui->actionUndo,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));
    QObject::connect(ui->actionRedo,SIGNAL(triggered()),ui->textEdit,SLOT(redo()));
    QObject::connect(ui->actionCut,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));
    QObject::connect(ui->actionCopy,SIGNAL(triggered()),ui->textEdit,SLOT(copy()));
    QObject::connect(ui->actionPaste,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
    QObject::connect(ui->actionSeleteAll,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));

    QObject::connect(ui->actionFont,SIGNAL(triggered()),this,SLOT(font()));
    QObject::connect(ui->actionColor,SIGNAL(triggered()),this,SLOT(colorFile()));
    QObject::connect(ui->actionDate,SIGNAL(triggered()),this,SLOT(datetimeFile()));
    QObject::connect(ui->actionAboutQt,SIGNAL(triggered(bool)),qApp,SLOT(aboutQt()));
    QObject::connect(ui->actionAboutWebsite,SIGNAL(triggered(bool)),this,SLOT(aboutWebsiteFun()));
    QObject::connect(ui->actionAboutUs,SIGNAL(triggered(bool)),this,SLOT(aboutUsFun()));

}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(ui->textEdit->document()->isModified())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("关闭确认");
        msgBox.setStandardButtons(QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
        msgBox.setButtonText(QMessageBox::Save,"保存");
        msgBox.setButtonText(QMessageBox::Discard,"不保存");
        msgBox.setButtonText(QMessageBox::Cancel,"放弃");

        msgBox.setInformativeText("你真的想保存吗？");
        msgBox.setText("关闭确认信息");

        int selectValue=msgBox.exec();
        if(selectValue==QMessageBox::Save)
        {
            this->saveFile();
        }
        else if(selectValue==QMessageBox::Discard)
        {
            event->accept();
        }
        else if(selectValue==QMessageBox::Cancel)
        {
            event->ignore();
        }

    }
    else
    {
        event->accept();
    }

}
void MainWindow::aboutUsFun()
{
    about *dialog = new about(this);
    dialog->show();

}
void MainWindow::aboutWebsiteFun()
{
    QDesktopServices::openUrl(QUrl("https://www.bitzh.edu.cn"));

}

void MainWindow::newFile()
{
    if(ui->textEdit->document()->isModified())
    {
        qDebug()<<"text modified.";
        ui->textEdit->clear();
        this->setWindowTitle("Untitled-notepad");
    }
    else
    {
        qDebug()<<"text is not modified.";
    }
}

void MainWindow::openFile()
{
    fileNameStr = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());
    if (fileNameStr.isEmpty()) {
        QMessageBox::information(this, "Error...", "Please select a file!");
        return;
    }
    QFile *file = new QFile(this);
    file->setFileName(fileNameStr);
    bool ok = false;
    ok = file->open(QIODevice::ReadOnly);
    if (ok) {
        QTextStream in(file);
        ui->textEdit->setText(in.readAll());
        file->close();
        delete file;
    }
    else {
        QMessageBox::information(this, "Error...", "Open file Error!");
    }
}

void MainWindow::saveFile()
{
    if(fileNameStr.isEmpty())
    {
        this->saveASFile();
    }
    else{
        QFile *file = new QFile(this);
        file->setFileName(fileNameStr);
        bool ok = file->open(QIODevice::WriteOnly);
        if(ok){
            QTextStream out(file);
            out << ui->textEdit->toPlainText();
            file->close();
            delete file;
        }
        else{
            QMessageBox::information(this,"Error...","Save file Error!");
        }
    }
}

void MainWindow::saveASFile()
{
    QString filePathStr = QFileDialog::getSaveFileName(this, "Save As...", QDir::currentPath());
    QFile *file = new QFile(this);
    file->setFileName(filePathStr);
    bool ok = file->open(QIODevice::WriteOnly);
    if (ok) {
        QTextStream out(file);
        out << ui->textEdit->toPlainText();
        file->close();
        delete file;
    }
    else {
        QMessageBox::information(this, "Error...", "Save file Error!");
    }
}

void MainWindow::printFile()
{
    QString printFilePath=QFileDialog::getSaveFileName(this,"Import the PDF file.","*.pdf");
    if(!printFilePath.isEmpty())
    {
        if(QFileInfo(printFilePath).suffix().isEmpty())
        {
            printFilePath.append(".pdf");
        }
        QPrinter printer;
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(printFilePath);
        ui->textEdit->print(&printer);
    }
}

void MainWindow::font()
{
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok)
    {
        ui->textEdit->setFont(font);
    }
    else
    {
        QMessageBox::information(this,"Error...","Font is not valid.");
    }

}


void MainWindow::colorFile()
{
    QColor color = QColorDialog::getColor(Qt::red,this);
    if(color.isValid())
    {

        ui->textEdit->setTextColor(color);
    }
    else
    {
        QMessageBox::warning(this,"ERROR...","Color is not valid!");

    }

}

void MainWindow::datetimeFile()
{
    QDateTime currentdatetime = QDateTime::currentDateTimeUtc();
//    currentdatetime=currentdatetime.addDays(1);
//    currentdatetime=currentdatetime.addMonths(1);
//    currentdatetime=currentdatetime.addYears(1);

    QString CDTstr = currentdatetime.toString("yyyy-MM-dd hh-mm-ss");
    ui->textEdit->append(CDTstr);
    CDTstr = currentdatetime.toString("yyyy-MM-dd MMMM hh-mm-ss A");
    ui->textEdit->append(CDTstr);
    QDateTime date1(QDate(2018,4,9),QTime(11,11,11));
    int validnumdays=30;
    QDateTime date2=date1.addDays(validnumdays);
    currentdatetime = QDateTime::currentDateTimeUtc();

    int n = currentdatetime.daysTo(date2);
    if(n<=0)
    {
        qDebug()<<"要过期了，请充值！！！";
    }
    else
    {
        qDebug()<<"谢谢使用，你的vip会员有效期还有"<<n<<"天";
    }



}











